package cn.iocoder.yudao.module.system.dal.mysql.a_pictures;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.a_pictures.vo.PicturesPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.a_pictures.PicturesDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface PicturesMapper extends BaseMapperX<PicturesDO> {

    /**
     * 分页查询
     * @param pageReqVO 查询参数
     * @return 返回列表
     */
    default PageResult<PicturesDO> page(PicturesPageReqVO pageReqVO){
        LambdaQueryWrapperX<PicturesDO> wrapperX = new LambdaQueryWrapperX<PicturesDO>()
                .likeIfPresent(PicturesDO::getKeyword, pageReqVO.getKeyword())       // 模糊查询关键词
                .likeIfPresent(PicturesDO::getContent, pageReqVO.getContent())       // 模糊查询描述
                .eqIfPresent(PicturesDO::getHeight, pageReqVO.getHeight())          // 精确查询高度
                .eqIfPresent(PicturesDO::getWidth, pageReqVO.getWidth())           // 精确查询宽度
                .eqIfPresent(PicturesDO::getCategory, pageReqVO.getCategory())     // 精确查询分类
                .eqIfPresent(PicturesDO::getIsShare, pageReqVO.getIsShare());       // 精确查询是否为共享素材库
        if(pageReqVO.getOnlyMe()){
            wrapperX.eq(PicturesDO::getCreator, String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
        }
        wrapperX.orderByDesc(PicturesDO::getCreateTime);                          // 按创建时间降序排序
        return selectPage(pageReqVO,wrapperX);
    }

    /**
     * 图片下载计数器
     * @param id
     * @return
     */
    @Update("UPDATE gj_pictures SET pictures_download_num = pictures_download_num + 1 WHERE id = #{id}")
    int downloadCount(String id);
}
